package com.hh.legalFactory.controller;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/27
 * @VERSON:1.8
 */

import com.hh.legalFactory.service.LegalFactoryService;
import com.hh.legalFactory.vo.AddLegalPlantVo;
import com.hh.legalFactory.vo.FormInlineVo;
import com.hh.legalFactory.vo.ModifyLegalPlantVo;
import com.hh.legalFactory.vo.UpdateStatusVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/legalPlant")
@CrossOrigin
public class LegalPlantController {
    @Autowired
    private LegalFactoryService legalFactoryService;

    @PostMapping("/queryFactory")
    public ResultVo queryFactory(@RequestBody FormInlineVo formInlineVo) {
        return ResultVo.success(legalFactoryService.findAllByPage(formInlineVo));
    }

    @PostMapping("/updateStatus")
    public ResultVo updateStatus(@RequestBody UpdateStatusVo updateStatusVo) {
        return legalFactoryService.updateStatus(updateStatusVo);
    }

    @GetMapping("/ofCurrentUser")
    public ResultVo ofCurrentUser() {
        return legalFactoryService.findAllLegalPersonName();
    }

    @PostMapping("/addLegalPlant")
    public ResultVo addLegalPlant(@RequestBody @Valid AddLegalPlantVo addLegalPlantVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalFactoryService.addFactory(addLegalPlantVo);
    }

    @PostMapping("/modifyLegalPlant")
    public ResultVo modifyLegalPlant(@RequestBody ModifyLegalPlantVo modifyLegalPlantVo) {
        return legalFactoryService.updateFactory(modifyLegalPlantVo);
    }
}
